package yxy.game.pm2.bean.module.rank.openserviceactive.herolevelrushrank;

import com.alibaba.fastjson.JSONObject;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.redisson.api.RBucket;
import yxy.apple.util.redisson.RedisConnection;
import yxy.apple.util.redisson.RedisException;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.actrushranking;
import yxy.cherry.data.bean.task;
import yxy.cherry.game.base.redis.RedisManager;
import yxy.cherry.game.base.redis.RedisType;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.AreaModule;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.openserviceactive.OpenServiceRushRank;
import yxy.game.pm2.bean.module.rank.openserviceactive.OpenServiceRushRankEnum;

import java.util.Collection;
import java.util.Map;
import java.util.Optional;

/**
 * 开服冲榜-武将等级-排行榜实现类
 *
 * @ClassName OpenServiceHeroLevelRushRank
 * @Author jingtao-zheng
 * @Date 2022/7/13 10:08
 * @Version 1.0
 **/
public class OpenServiceHeroLevelRushRank extends OpenServiceRushRank {


    public OpenServiceHeroLevelRushRank(int areaId, int type) {
        super(areaId, type);
    }

    /**
     * 更新武将等级排行榜
     */
    static public int[] updateHeroLevelRanking(String playerCode, int aptitude, int level, int rankingType) {
        AreaBean areaBean = AreaModule.area(playerCode);
        int areaId = areaBean.getAreaId();
        OpenServiceHeroLevelRushRank osrk = new OpenServiceHeroLevelRushRank(areaId, rankingType);
        //计算活动是否开启
        boolean extracted = extracted(playerCode, rankingType);
        if (!extracted) {
            int[] result = new int[3];
            result[2] = -1;
            return result;
        }
        //计算武将资质是否满足策划配置要求
        int[] isupdate = osrk.isUpdateHeroLevelRanking(playerCode, aptitude, level, rankingType);

        if (isupdate[2] == 1) {
            osrk.addAndSet(playerCode, isupdate[1]);
        }
        return isupdate;
    }

    private static boolean extracted(String playerCode, int rankingType) {
        RedisConnection connection = RedisManager.getInstance().getConnection(RedisType.REDIS_DATA);
        JSONObject json = null;
        try {
            /**
             * 由于拿不到活动控制类
             * 暂时只能通过key拿出json来用
             */
            AreaBean areaBean = AreaModule.area(playerCode);
            int areaId = areaBean.getAreaId();
            RBucket<Object> bucket = RedisManager.client(connection)
                    .getBucket(String.format("AREA:%s:ACTTYPE:%s", areaId, rankingType));
            Object o = bucket.get();
            json = (JSONObject) JSONObject.toJSON(o);
            //防止其他区没开活动而报错
            if(null == json){
                return false;
            }
        } catch (RedisException e) {
            e.printStackTrace();
        }
        //获取当前时间
        DateTime now = DateTime.now();
        /**
         *  获取活动开始时间
         */
        //获取策划配置的开始时间和结束时间
        DateTime chStartTime = DateTime.parse((String) ((JSONObject) json.get("timeline")).get("startTime"));
        DateTime chendTime = DateTime.parse((String) ((JSONObject) json.get("timeline")).get("endTime"));
        //获取开服时间
        PlayerBean playerBean = PlayerBean.get(playerCode);
        ServerBean serverBean = ServerBean.get(playerBean.getServerId());
        DateTime starttime = serverBean.getStarttime();

        String s = starttime.toString("yyyy-MM-dd");
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
        starttime = DateTime.parse(s, format);

        //活动开启条件
        int daycondition = (int) json.get("daycondition");
        //计算真实活动开始时间=开服时间 + 活动开启条件表示的天数
        DateTime realStartDateTime = starttime.plusDays(daycondition);
        /**
         * 获取活动结束时间
         */
        //活动运行天数
        int operationcycle = (int) json.get("operationcycle");
        ;
        // 真实活动结束时间 = 真实活动开始时间 + 活动运行天数
        DateTime realEndDateTime = realStartDateTime.plusDays(operationcycle);

        //计算是否开启活动
        return chStartTime.getMillis() <= now.getMillis()
                &&
                now.getMillis() <= chendTime.getMillis()
                &&
                realStartDateTime.getMillis() <= now.getMillis()
                &&
                now.getMillis() <= realEndDateTime.getMillis()
                ;

    }


    /**
     * 是否更新排行榜
     */
    private int[] isUpdateHeroLevelRanking(String playerCode, int aptitude,int level,int rankingType) {
        //下标0为资质 下标1为等级差  下标3为是否满足要求
        int[] result = new int[3];

        //获取武将等级的任务id
        Map<String, actrushranking> datamap = DataCenter.getAllData(actrushranking.class);
        Optional<actrushranking> first = datamap.values().stream().filter(v -> v.getGroup() == 3).findFirst();
        actrushranking actrushranking = first.get();
        int taskid = actrushranking.getTaskid();
        //获取策划配置的资质等级
        task taskexecl = DataCenter.getData(taskid, task.class);
        int[] finishref = taskexecl.getFinishref();
        //资质大于策划配置的资质，就看武将等级是不是当前玩家的冲榜武将的最高等级
        if(aptitude >= finishref[1]){
            RankEntry rankEntry = get(playerCode, OpenServiceRushRankEnum.开服冲榜_等级提升.getType());
            long value = 0;
            if(null != rankEntry){
                value = rankEntry.getValue();
            }
            if(value < level){
                result[0] = finishref[1];
                int leveltemp = level - (int) value;
                result[1] = leveltemp;
                result[2] = 1;
                return result;
            }
        }
        result[2] = -1;
        return result;
    }

    /**
     * 获取排名信息
     */
    static public Collection<RankEntry> top(String playercode, int rankrushtype, int size) {
        AreaBean areaBean = AreaModule.area(playercode);
        int areaId = areaBean.getAreaId();
        OpenServiceHeroLevelRushRank cbrr = new OpenServiceHeroLevelRushRank(areaId, rankrushtype);
        return cbrr.top(size);
    }

    /**
     * 获取一定范围的排名信息
     */
    static public Collection<RankEntry> range(int areaid, int rankrushtype, int min, int max) {
        OpenServiceHeroLevelRushRank cbrr = new OpenServiceHeroLevelRushRank(areaid, rankrushtype);
        return cbrr.range(min, max);
    }

    /**
     * 获取单人信息
     */
    static public RankEntry get(String playercode, int rankrushtype) {
        AreaBean areaBean = AreaModule.area(playercode);
        int areaId = areaBean.getAreaId();
        OpenServiceHeroLevelRushRank cbrr = new OpenServiceHeroLevelRushRank(areaId, rankrushtype);
        RankEntry rankEntry = cbrr.get(playercode);
        return rankEntry;
    }

    /**
     * 获取单人排名
     */
    static public int rank(String playercode, int rankrushtype) {
        AreaBean areaBean = AreaModule.area(playercode);
        int areaId = areaBean.getAreaId();
        OpenServiceHeroLevelRushRank cbrr = new OpenServiceHeroLevelRushRank(areaId, rankrushtype);
        int rank = cbrr.rank(playercode);
        return rank;
    }
}
